package com.tridion.utilities.verifier.reports;

import java.util.List;

import com.tridion.utilities.verifier.model.ContentDeliveryInstallation;
import com.tridion.utilities.verifier.model.JarFile;

/**
 * Report that exposes information about an installation in a simple textual way.
 * 
 * @author Daniel Neagu
 */
public class SimpleTextualReport {

    public static void generateReport(final ContentDeliveryInstallation cdi) {
        final StringBuilder report = new StringBuilder();
        
        report.append("Application type: ").append(cdi.getType()).append("\n");
        report.append("Application root location: ").append(cdi.getRootLocation()).append("\n");
        report.append("Application lib folder: ").append(cdi.getLibFolder()).append("\n");
        report.append("Application config folder: ").append(cdi.getConfigFolder()).append("\n").append("\n");
        
        if (cdi.getRelease() != null) {
            report.append("Application version: ").append(cdi.getRelease().getName());
            
            if (cdi.getMinorRelease() != null) {
                report.append("-").append(cdi.getMinorRelease().getName());
            }
            
            report.append(" (").append(cdi.getRelease().getVersion());
            if (cdi.getMinorRelease() != null) {
                report.append("-").append(cdi.getMinorRelease().getName());
            }
            
            report.append(")").append("\n").append("\n");
        } else {
            report.append("Unknown release!!!").append("\n").append("\n");
        }
        
        
        report.append("List of existing jars in the lib folder: ").append("\n");
        report.append("===========================================================================").append("\n");
        report.append("===========================================================================").append("\n");
        final List<JarFile> jarsVersion = cdi.getJars();
        for (JarFile jar : jarsVersion) {
            final String name = jar.getName();
            final String version = jar.getVersion();

            report.append(name).append(": ");

            final int nameLength = name.length();
            final int filling = 40 - nameLength;
            for (int i = 0; i < filling; i++) {
                report.append(" ");
            }
            
            report.append(version);
            
            if (jar.getStatusMessage() != null) {
                final int versionLength = version.length();
                final int filling1 = 20 - versionLength;
                for (int i = 0; i < filling1; i++) {
                    report.append(" ");
                }
                
                report.append(jar.getStatusMessage().getType());
                report.append(" ");
                report.append(jar.getStatusMessage().getMessage());
            }
            
            report.append("\n");
        }
        report.append("===========================================================================").append("\n");
        report.append("===========================================================================").append("\n");
        
        
        System.out.println(report.toString());
    }
}
